package com.example.springblog.service;

import com.example.springblog.mapper.BlogInfoMapper;
import com.example.springblog.mapper.UserInfoMapper;
import com.example.springblog.model.BlogInfo;
import com.example.springblog.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public UserInfo selectByName(String userName){
        UserInfo userInfo=userInfoMapper.selectByName(userName);
        return userInfo;
    }

    public UserInfo selectById(Integer id){
        UserInfo userInfo = userInfoMapper.selectByID(id);
        return userInfo;
    }

    public UserInfo selectByBlogId(Integer blogId){
        //通过博客ID查询博客信息
        BlogInfo blogInfo = blogInfoMapper.selectById(blogId);
        //如果存在博客信息不存在
        if(blogInfo == null && blogInfo.getUserId()<=0){
            log.error("博客信息不存在，或者用户id错误");
            return null;
        }
        //根据博客信息查询作者信息
        UserInfo userInfo = userInfoMapper.selectByID(blogInfo.getUserId());

        return userInfo;
    }
}
